package com.hai.aidldemo;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.hai.server.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    IMyAidlInterface myAidlInterface;
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected() called with: name = [" + name + "], service = [" + service + "]");
            myAidlInterface = IMyAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected() called with: name = [" + name + "]");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
    }

    public void clk(View view) {
        switch (view.getId()) {
            case R.id.btn_bind:
                if (myAidlInterface == null) {
                    boolean b = bindService(new Intent("com.hai.server.ServerService").setPackage("com.hai.server"), serviceConnection, Context.BIND_AUTO_CREATE);
                    Log.d(TAG, "bindService: " + b);
                }
                break;
            case R.id.btn_unbind:
                if (myAidlInterface != null) {
                    unbindService(serviceConnection);
                }
                break;
            case R.id.btn_basicType:
                try {
                    myAidlInterface.basicTypes(1, 2, true, 4, 5, "6");
                } catch (RemoteException e) {
                    throw new RuntimeException(e);
                }
                break;
            case R.id.btn_getPid:
                try {
                    long start = System.currentTimeMillis();
                    int pid = myAidlInterface.getPid();
                    Log.d(TAG, "getPid: " + pid + ", cost:" + (System.currentTimeMillis() - start));
                } catch (RemoteException e) {
                    throw new RuntimeException(e);
                }
                break;
            case R.id.btn_getThreadId:
                try {
                    long start = System.currentTimeMillis();
                    myAidlInterface.getThreadId();
                    Log.d(TAG, "getThreadId: cost:" + (System.currentTimeMillis() - start));
                } catch (RemoteException e) {
                    throw new RuntimeException(e);
                }
                break;
        }
    }
}